package com.gjy.disruptor.excel;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.sql.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-08-07 12:22:14
 */
public class ExcelDisruptor {


    public static void main(String[] args) {


        int ringBufferSize = 1024 * 1024;
        Disruptor<ExcelEntity> disruptor = new Disruptor<>(new ExcelEventFactory(), ringBufferSize, Executors.defaultThreadFactory(),
                ProducerType.SINGLE, new YieldingWaitStrategy());

        disruptor.handleEventsWith(new ExcelConsumer(1000000));
        disruptor.start();

        ExecutorService pool = Executors.newSingleThreadExecutor();
        pool.submit(() -> {
            RingBuffer<ExcelEntity> ringBuffer = disruptor.getRingBuffer();
            String sql = "select id,name from disruptor_t1";
            try {
                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/liteflow?rewriteBatchedStatements=true&cachePrepStmts=true&prepStmtCacheSize=250&prepStmtCacheSqlLimit=2048", "root", "Oracle@12");
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    long id = rs.getLong("id");
                    String name = rs.getString("name");
                    ringBuffer.publishEvent(new ExcelEventTranslator(id, name));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        });
    }
}
